//防抖函数
export const debounce = (fn, t) => {
  const delay = t || 500
  let timer
  // eslint-disable-next-line func-names
  return function () {
    // eslint-disable-next-line prefer-rest-params
    const args = arguments
    if (timer) {
      clearTimeout(timer)
    }
    const context = this
    timer = setTimeout(() => {
      timer = null
      fn.apply(context, args)
    }, delay)
  }
}
//表格滚动
export const tableScoll = (ref) => {
  const divData = ref.getElementsByClassName('el-scrollbar__wrap')[0]
  const interval = setInterval(() => {
    divData.scrollTop += 1
    if (divData.clientHeight + divData.scrollTop == divData.scrollHeight) {
      divData.scrollTop = 0
    }
  }, 50)

  return interval
}
//格式化时间
let week = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']
export const formatTime = (date, type) => {
  if (!date) {
    date = new Date()
  }
  // let date = new Date()
  let year = date.getFullYear()
  let month = date.getMonth() + 1
  month = month > 9 ? month : '0' + month
  let day = date.getDate()
  day = day > 9 ? day : '0' + day
  let hour = date.getHours()
  hour = hour > 9 ? hour : '0' + hour
  let minute = date.getMinutes()
  minute = minute > 9 ? minute : '0' + minute
  let second = date.getSeconds()
  second = second > 9 ? second : '0' + second
  let weekDay = date.getDay()
  weekDay = week[weekDay]
  if (type === 'timeNow') {
    return `${hour}:${minute}:${second}`
  } else if (type === 'dateNow') {
    return `${year}-${month}-${day}`
  } else if (type === 'hpTime') {
    return `${month}月${day}日 ${weekDay}`
  } else if (type == 'entireTime') {
    return `${year}/${month}/${day} ${hour}:${minute}:${second}`
  } else if (type == 'carTime') {
    return `${year}-${month}-${day} ${hour}:${minute}:${second}`
  } else if ('lxm') {
    return `${hour}:${minute}:${second}${year}.${month}.${day} ${weekDay}`
  }
}
export const deepClone = (obj) => {
  let objClone = Array.isArray(obj) ? [] : {}
  if (obj && typeof obj === 'object') {
    for (var key in obj) {
      if (obj.hasOwnProperty(key)) {
        //判断ojb子元素是否为对象，如果是，递归复制
        if (obj[key] && typeof obj[key] === 'object') {
          objClone[key] = deepClone(obj[key])
        } else {
          //如果不是，简单复制
          objClone[key] = obj[key]
        }
      }
    }
  }
  return objClone
}
